﻿<documents created="2010-12-15 11:41:46Z"><document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Automaton Model\RuleLib\GameOfLife.cs" renderPrefix="s0"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.Pex.Framework;
using Microsoft.Pex.Framework.Validation;

namespace AutomaTones.Model.Automaton_Model.RuleLib {
    /// <author>Anders Øland / anoe@itu</author>
    /// <summary>
    /// Provides the rule used for the classic CA called Game of Life - invented by Jonh Conway in the 1970's.<br></br><br></br>
    /// This simple rule generates an impressive diversity of behaviour:
    /// If a black cell has 2 or 3 black neighbours, it stays black. If a black cell has less than 2 or more than 3
    /// black neighbours it becomes white. If a white cell has 3 black neighbours, it becomes black.
    /// <br></br><br></br>
    /// This class also facilitates easy setup of a Game of Life CA, as well as the adding of known patterns to a CA. 
    /// </summary>
    [PexClass]
    public static partial class GameOfLife {
        /// <summary>
        /// The rule used for the classic CA called Game of Life.<br></br><br></br> 
        /// If a black cell has 2 or 3 black neighbours, it stays black. If a black cell has less than 2 or more than 3
        /// black neighbours it becomes white. If a white cell has 3 black neighbours, it becomes black.
        /// <br></br><br></br>
        /// This particular rule has been modified so it can handle state spaces which are not binary.
        /// Thus, if a live cell stays alive in the next generation its state is increased with one.
        /// If the new state > space max ==> new state = 1
        /// </summary>
        public static int GameOfLifeRule(Cell cell, ref IEnumerable<Rule.StateAtCoordinate> statesOfNeighbours,
                                         params int[] unused) {
            Contract.Requires(null != cell);
            Contract.Requires(0 == cell.State.SpaceMin);
            Contract.Requires(1 == cell.State.SpaceMax);
            Contract.Requires(Contract.ForAll(statesOfNeighbours, sn => 2 == sn.Position.Coord.Length));
            Contract.Requires(Contract.ForAll(statesOfNeighbours, sn => cell.State.SpaceMin <= sn.State
                                                                        && cell.State.SpaceMax >= sn.State));
            Contract.Ensures(cell.State.SpaceMin <= Contract.Result<int>());
            Contract.Ensures(cell.State.SpaceMax >= Contract.Result<int>());
            var blackNeighbours = (from n in statesOfNeighbours
                                   where n.State != 0
                                   select n).Count();

            var newState = 0;
            if (0 != cell.State.CurrentState)
                newState = (blackNeighbours == 2 || blackNeighbours == 3) ? 1 : 0;
            else newState = blackNeighbours == 3 ? 1 : 0;
            newState = newState == 0 ? 0 : 1 + cell.State.CurrentState;
            return newState > cell.State.SpaceMax ? 1 : newState;
        }

        /// <summary>
        /// Sets up a Cellular Automaton for Game of Life.
        /// </summary>
        /// <param name="size">Size of the dimensions of the rectangular 2-dimensional grid</param>
        /// <param name="stateSpace"></param>
        /// <returns>Game of Life CA</returns>
        [PexMethod, PexAllowedException(typeof(OverflowException))]
        public static CellularAutomaton SetupGameOfLife(uint size, uint stateSpace = 2u) {
            Contract.Requires(0 < size);
            Contract.Requires(1 < stateSpace);
            Contract.Ensures(size == Contract.Result<CellularAutomaton>().DimensionSize);
            Contract.Ensures(2 == Contract.Result<CellularAutomaton>().Dimensions);
            // Create a Rule instance
            Rule gofRule = GetGofRule();

            // Create a CellularAutomaton instance
            CellularAutomaton gameOfLifeCA = new CellularAutomaton(2, size, gofRule, 0, (int) stateSpace - 1);
            return gameOfLifeCA;
        }

        /// <summary>
        /// Get a new GOF Rule instance.
        /// </summary>
        /// <returns></returns>
        public static Rule GetGofRule() {
            Contract.Ensures(8 == Contract.Result<Rule>().Neighbourhood.Count);
            Contract.Ensures(2 == Contract.Result<Rule>().Dimensions);
            Contract.Ensures(GameOfLifeRule == Contract.Result<Rule>().MyRuleDelegate);
            Rule gofRule = new Rule(2, GameOfLifeRule);

            // Setup the coordinates for the neighbours which the rule uses 
            var neighbourhood = new[] {
                                          new[] {-1, 1}, new[] {0, 1}, new[] {1, 1}, new[] {1, 0},
                                          new[] {1, -1}, new[] {0, -1}, new[] {-1, -1}, new[] {-1, 0}
                                      };

            foreach (int[] n in neighbourhood) gofRule.AddNeighbour(n);
            return gofRule;
        }

        /// <summary>
        /// Demo game of life in the console.
        /// Press a key + return to evolve.
        /// </summary>
        public static void GameOfLifeDemo(GofPattern pattern, int g) {
            CellularAutomaton gameOfLifeCA = SetupGameOfLife(75, 2);

            AddGofPattern(gameOfLifeCA, pattern, 20, 20);

            while (0 < g--) {
                Console.Clear();
                Console.WriteLine(gameOfLifeCA + "\n");
                gameOfLifeCA.Evolve();
//                Console.Read();
            }
// ReSharper disable FunctionNeverReturns
        }
// ReSharper restore FunctionNeverReturns

        /// <summary>
        /// Adds a GofPattern to the Game of Life automaton, centered around the given coordinate.
        /// </summary>
        /// <param name="gofAutomaton"></param>
        /// <param name="gofPattern"></param>
        /// <param name="xPos"></param>
        /// <param name="yPos"></param>
        [PexMethod]
        public static void AddGofPattern(CellularAutomaton gofAutomaton, GofPattern gofPattern, int xPos = 0,
                                         int yPos = 0) ]]></source>
</chunk>
<method name="AddGofPattern" namespace="AutomaTones.Model.Automaton_Model.RuleLib" type="AutomaTones.Model.Automaton_Model.RuleLib.GameOfLife" typeName="GameOfLife" token="6000098" module="AutomaTones.exe"><hits><hit index="0" value="1"/>
<hit index="1" value="1"/>
<hit index="2" value="1"/>
<hit index="3" value="1"/>
<hit index="4" value="1"/>
<hit index="5" value="1"/>
<hit index="6" value="1"/>
<hit index="7" value="1"/>
<hit index="8" value="1"/>
<hit index="9" value="1"/>
<hit index="10" value="1"/>
<hit index="12" value="21"/>
<hit index="13" value="21"/>
<hit index="14" value="21"/>
<hit index="15" value="21"/>
<hit index="16" value="21"/>
<hit index="17" value="21"/>
<hit index="18" value="21"/>
<hit index="19" value="21"/>
<hit index="20" value="21"/>
<hit index="21" value="21"/>
<hit index="22" value="21"/>
<hit index="23" value="21"/>
<hit index="24" value="21"/>
<hit index="25" value="42"/>
<hit index="26" value="3"/>
<hit index="27" value="1"/>
<hit index="28" value="21"/>
<hit index="29" value="3"/>
<hit index="30" value="1"/>
<hit index="31" value="1"/>
<hit index="34" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="6" total="6" coveredRatio="1"/>
</method>
<chunk hits="1" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(null != gofAutomaton);
            Contract.Requires(2 == gofAutomaton.Dimensions);
            ]]></source>
</chunk>
<chunk hits="1" offset="0001" domain="userortestcode"><source><![CDATA[int[] pattern = Patterns[(int) gofPattern];]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="0009" domain="userortestcode"><source><![CDATA[int xSize = pattern[0],]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="2" offset="000d" domain="userortestcode"><source><![CDATA[ySize = pattern[1];]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="0011" domain="userortestcode"><source><![CDATA[int deltaX = xSize / 2,]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="2" offset="0015" domain="userortestcode"><source><![CDATA[deltaY = ySize / 2;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

            for (]]></source>
</chunk>
<chunk hits="2" offset="001a" domain="userortestcode"><source><![CDATA[int y = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="5" offset="0070" domain="userortestcode"><source><![CDATA[y < ySize]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[; ]]></source>
</chunk>
<chunk hits="3" offset="006a" domain="userortestcode"><source><![CDATA[y++]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[) ]]></source>
</chunk>
<chunk hits="3" offset="001f" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                for (]]></source>
</chunk>
<chunk hits="3" offset="0020" domain="userortestcode"><source><![CDATA[int x = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="66" offset="005e" domain="userortestcode"><source><![CDATA[x < xSize]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[; ]]></source>
</chunk>
<chunk hits="63" offset="0058" domain="userortestcode"><source><![CDATA[x++]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[) ]]></source>
</chunk>
<chunk hits="21" offset="0025" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                    ]]></source>
</chunk>
<chunk hits="21" offset="0026" domain="userortestcode"><source><![CDATA[gofAutomaton[x - deltaX + xPos, y - deltaY + yPos] = pattern[2 + x + y * xSize];]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="63" offset="0057" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="3" offset="0069" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="1" offset="007b" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// A collection of interesting patterns for Game of Life.
        /// To add them to your cellular automaton use the AddGofPattern method.
        /// </summary>
        public enum GofPattern {
            Blinker,
            Toad,
            Beacon,
            Pulsar,
            Block,
            Beehive,
            Loaf,
            Boat,
            Glider,
            LightweightSpaceship,
            RPentomino,
            Diehard,
            Acorn,
            InfiniteGrowth1,
            InfiniteGrowth2,
            InfiniteGrowth3,
            GosperGliderGun
        }

// Oscillator Patterns:
        public static readonly int[] Blinker = new[] {
                                                         // Dimensions
                                                         3, 1,
                                                         // Pattern
                                                         1, 1, 1
                                                     };

        public static readonly int[] Toad = new[] {
                                                      // Dimensions                                
                                                      4, 2,
                                                      // Pattern
                                                      0, 1, 1, 1,
                                                      1, 1, 1, 0
                                                  };

        public static readonly int[] Beacon = new[] {
                                                        // Dimensions                                
                                                        4, 4,
                                                        // Pattern
                                                        1, 1, 0, 0,
                                                        1, 0, 0, 0,
                                                        0, 0, 0, 1,
                                                        0, 0, 1, 1,
                                                    };

        public static readonly int[] Pulsar = new[] {
                                                        // Dimensions
                                                        15, 15,
                                                        // Pattern
                                                        0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                                                        0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                                                        0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
                                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                        1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1,
                                                        0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0,
                                                        0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
                                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                        0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
                                                        0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0,
                                                        1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1,
                                                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                                        0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0,
                                                        0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
                                                        0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0
                                                    };

// Still Lives Patterns:
        public static readonly int[] Block = new[] {
                                                       // Dimensions                                
                                                       2, 2,
                                                       // Pattern
                                                       1, 1,
                                                       1, 1
                                                   };

        public static readonly int[] Beehive = new[] {
                                                         // Dimensions                                
                                                         4, 3,
                                                         // Pattern
                                                         0, 1, 1, 0,
                                                         1, 0, 0, 1,
                                                         0, 1, 1, 0
                                                     };

        public static readonly int[] Loaf = new[] {
                                                      // Dimensions                                
                                                      4, 4,
                                                      // Pattern
                                                      0, 1, 1, 0,
                                                      1, 0, 0, 1,
                                                      0, 1, 0, 1,
                                                      0, 0, 1, 0,
                                                  };

        public static readonly int[] Boat = new[] {
                                                      // Dimensions                                
                                                      3, 3,
                                                      // Pattern
                                                      1, 1, 0,
                                                      1, 0, 1,
                                                      0, 1, 0,
                                                  };

// Spacehip Patterns:
        public static readonly int[] Glider = new[] {
                                                        // Dimensions                                
                                                        3, 3,
                                                        // Pattern
                                                        0, 1, 0,
                                                        0, 0, 1,
                                                        1, 1, 1,
                                                    };

        public static readonly int[] LightweightSpaceship = new[] {
                                                                      // Dimensions                                
                                                                      5, 4,
                                                                      // Pattern
                                                                      1, 0, 0, 1, 0,
                                                                      0, 0, 0, 0, 1,
                                                                      1, 0, 0, 0, 1,
                                                                      0, 1, 1, 1, 1
                                                                  };

// Methuselah Patterns - can evolve for a long time before stabilizing
        /// <summary>
        /// Methuselah Pattern - can evolve for a long time before stabilizing.
        /// </summary>
        public static readonly int[] RPentomino = new[] {
                                                            // Dimensions                                
                                                            4, 4,
                                                            // Pattern
                                                            0, 1, 1, 0,
                                                            1, 0, 0, 1,
                                                            0, 1, 0, 1,
                                                            0, 0, 1, 0,
                                                        };

        /// <summary>
        /// Methuselah Pattern - can evolve for a long time before stabilizing.
        /// This one disappears after 130 generations
        /// </summary>
        public static readonly int[] Diehard = new[] {
                                                         // Dimensions                                
                                                         8, 3,
                                                         // Pattern
                                                         0, 0, 0, 0, 0, 0, 1, 0,
                                                         1, 1, 0, 0, 0, 0, 0, 0,
                                                         0, 1, 0, 0, 0, 1, 1, 1,
                                                     };

        /// <summary>
        /// Methuselah Pattern - can evolve for a long time before stabilizing.
        /// This one disappears after 130 generations
        /// </summary>
        public static readonly int[] Acorn = new[] {
                                                       // Dimensions                                
                                                       7, 3,
                                                       // Pattern
                                                       0, 1, 0, 0, 0, 0, 1,
                                                       0, 0, 0, 1, 0, 0, 0,
                                                       1, 1, 0, 0, 1, 1, 1
                                                   };

// Infinite Growth Patterns:
        /// <summary>
        /// Infinite Growth Pattern - with only 10 live cell (which proven to be mnimal)
        /// </summary>
        public static readonly int[] InfiniteGrowth1 = new[] {
                                                                 // Dimensions                                
                                                                 8, 6,
                                                                 // Pattern
                                                                 0, 0, 0, 0, 0, 0, 1, 0,
                                                                 0, 0, 0, 0, 1, 0, 1, 1,
                                                                 0, 0, 0, 0, 1, 0, 1, 0,
                                                                 0, 0, 0, 0, 1, 0, 0, 0,
                                                                 0, 0, 1, 0, 0, 0, 0, 0,
                                                                 1, 0, 1, 0, 0, 0, 0, 0
                                                             };

        /// <summary>
        /// Infinite Growth Pattern - from a mere 5x5 pattern
        /// </summary>
        public static readonly int[] InfiniteGrowth2 = new[] {
                                                                 // Dimensions                                
                                                                 5, 4,
                                                                 // Pattern
                                                                 1, 1, 1, 0, 1,
                                                                 1, 0, 0, 0, 0,
                                                                 0, 0, 0, 1, 1,
                                                                 0, 1, 1, 0, 1,
                                                                 1, 0, 1, 0, 1
                                                             };

        /// <summary>
        /// Infinite Growth Pattern - only one cell high
        /// </summary>
        public static readonly int[] InfiniteGrowth3 =
            new[] {
                      // Dimensions                                
                      39, 1,
                      // Pattern
                      1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1
                      , 1, 0, 1, 1, 1, 1, 1
                  };

        /// <summary>
        /// The World's first Infinite Growth Pattern - Gosper Glider Gun.
        /// Developed at MIT in 1970 by a team lead by Bill Gosper.
        /// Proved for the first time that infinite growth is possbile in Game of Life.
        /// ...They won a $50 prize from John Conway (the inventor of Game of Life (and apparently a cheapskate?).
        /// </summary>
        public static readonly int[] GosperGliderGun =
            new[] {
                      // Dimensions                                
                      36, 9,
                      // Pattern
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 1, 1,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 1, 1,
                      1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 0, 0,
                      1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                      , 0, 0, 0, 0,
                  };

        private static readonly int[][] Patterns = new[] {
                                                             Blinker,
                                                             Toad,
                                                             Beacon,
                                                             Pulsar,
                                                             Block,
                                                             Beehive,
                                                             Loaf,
                                                             Boat,
                                                             Glider,
                                                             LightweightSpaceship,
                                                             RPentomino,
                                                             Diehard,
                                                             Acorn,
                                                             InfiniteGrowth1,
                                                             InfiniteGrowth2,
                                                             InfiniteGrowth3,
                                                             GosperGliderGun
                                                         };
    }
}]]></source>
</chunk>
</document>
<document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Automaton Model\Cellular Automaton.cs" renderPrefix="s1"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.Pex.Framework;

namespace AutomaTones.Model.Automaton_Model {
    /// <summary>
    ///   A cellular automaton consisting of an N-dimensional grid of cells.
    ///   Every new generation each cell gets a given state defined by one or more rules.
    ///   These rules consider a predefined set of neighbouring cells and hence decide a cells new state.
    ///   States are always represented using integers (discrete values).
    ///   States can be set and read via an indexer, eg. ca[0, 1] = 1;<br></br><br></br>
    ///   NOTE: Ideally this would be implemented using generics so states could also be decimals.
    ///   But due to C#'s lack of type constraint options on value types - this was omitted.<br></br><br></br>
    ///   This implementation is designed to allow a high degree of flexibility in the use of cellular automata.
    ///   Its N-dimensional and each cell may have its own state space and rule(s). Rules can use different
    ///   neighbourhoods which they process with their RuleDelegate - which again may use other delegates.
    ///   Thus this flexibility may make the overall semantics a little confusing at first.
    ///   However, one can setup a simple CA where all cells use the same space, rule and neighbourhood 
    ///   by simply using one of the constructors.
    /// </summary>
    /// <author>Anders Øland / anoe@itu.dk</author>
    [PexClass]
    [Serializable]
    public class CellularAutomaton : ICloneable {
        public uint Dimensions { get; private set; }
        public uint DimensionSize { get; private set; }
        public uint Generation { get; private set; }

        // _grid hold the cells of CA's the N-dimensional grid
        protected Cell[] Grid;

        public Rule DefaultRule { get; private set; }

        /// <summary>
        ///   Construct a new Cellular Automaton
        /// </summary>
        /// <param name = "dimensions">Number of dimensions</param>
        /// <param name = "dimensionSize">Size of the dimensions</param>
        /// <param name = "defaultRule">The default rule for all cells</param>
        /// <param name = "stateMin">default = 0</param>
        /// <param name = "stateMax">default = 1</param>
        /// <param name = "stateInit"></param>
//        [PexMethod]
        ]]></source>
</chunk>
<method name=".ctor" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" typeName="CellularAutomaton" token="600013a" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="3" coveredRatio="0"/>
</method>
<chunk hits="0" offset="0000" domain="userortestcode"><source><![CDATA[public CellularAutomaton(uint dimensions, uint dimensionSize, Rule defaultRule, int stateMin = 0,
                                 int stateMax = 1, int stateInit = 0)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0007" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(0 < dimensions);
            Contract.Requires(0 < dimensionSize);
            Contract.Requires(null != defaultRule);
            Contract.Requires(dimensions == defaultRule.Dimensions);
            Contract.Requires(stateMin < stateMax);
            Contract.Requires(stateMin <= stateInit && stateMax >= stateInit);
            ]]></source>
</chunk>
<chunk hits="0" offset="0008" domain="userortestcode"><source><![CDATA[Init(dimensions, dimensionSize, defaultRule, stateMin, stateMax, stateInit);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="0018" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        private void Init(uint dimensions, uint dimensionSize, Rule defaultRule,
                          int stateMin = 0, int stateMax = 1, int stateInit = 0) ]]></source>
</chunk>
<method name="Init" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" typeName="CellularAutomaton" token="600013b" module="AutomaTones.exe"><hits/>
<tags><tag offset="002a">call to external method Math.Pow(Double, Double)</tag>
<tag offset="0030">call to uninstrumented method __LimitationsHelper.floating_point_conversion(), call to uninstrumented method __LimitationsHelper.floating_point_multiplication()</tag>
</tags>
<coverage domain="userortestcode" unit="block" covered="0" total="11" coveredRatio="0"/>
</method>
<chunk hits="0" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(0 < dimensions && 0 < dimensionSize);
            Contract.Requires(dimensions == defaultRule.Dimensions);
            Contract.Requires(stateMin < stateMax);
            Contract.Requires(stateMin <= stateInit && stateMax >= stateInit);
            ]]></source>
</chunk>
<chunk hits="0" offset="0001" domain="userortestcode"><source><![CDATA[Generation = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0009" domain="userortestcode"><source><![CDATA[Dimensions = dimensions;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0011" domain="userortestcode"><source><![CDATA[DimensionSize = dimensionSize;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0019" domain="userortestcode" tag="true"><tooltip><![CDATA[call to external method Math.Pow(Double, Double), call to uninstrumented method __LimitationsHelper.floating_point_conversion(), call to uninstrumented method __LimitationsHelper.floating_point_multiplication()]]></tooltip>
<source><![CDATA[Grid = new Cell[(int)Math.Pow(DimensionSize, Dimensions)];]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            for (]]></source>
</chunk>
<chunk hits="0" offset="003a" domain="userortestcode"><source><![CDATA[int i = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0058" domain="userortestcode"><source><![CDATA[i < Grid.Length]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[; ]]></source>
</chunk>
<chunk hits="0" offset="0054" domain="userortestcode"><source><![CDATA[i++]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[) ]]></source>
</chunk>
<chunk hits="0" offset="003e" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="0" offset="003f" domain="userortestcode"><source><![CDATA[Grid[i] = new Cell(defaultRule, stateMin, stateMax, stateInit);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0053" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0067" domain="userortestcode"><source><![CDATA[DefaultRule = defaultRule;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="006f" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        ///   Finds the right cell in the N-dimensional grid in the one dimensional array holding the cells
        /// </summary>
        [PexMethod]
        [Pure] // Tagged as pure since its being called from other contracts which I dont want to loosen 
        protected internal virtual Cell GetCell(params int[] coord) {
            Contract.Requires(null != coord);
            Contract.Requires(coord.Length == Dimensions);
            Contract.Ensures(Contract.Result<Cell>() != null);
            uint index = GetIndex(coord);
            return Grid[index];
        }

        /// <summary>
        ///   Translates a coordinate into an index number pointing to the corresponing location in the grid array.
        ///   Coordinates exceeding the bounds of the grid are 'wrapped around', 
        ///   thus giving the grid a so-called torus shape.
        /// </summary>
        [Pure]
        protected uint GetIndex(int[] coord) ]]></source>
</chunk>
<method name="GetIndex" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" typeName="CellularAutomaton" token="600013d" module="AutomaTones.exe"><hits><hit index="0" value="21"/>
<hit index="1" value="3"/>
<hit index="2" value="3"/>
<hit index="3" value="3"/>
<hit index="4" value="3"/>
<hit index="5" value="3"/>
<hit index="6" value="3"/>
<hit index="7" value="2"/>
<hit index="8" value="2"/>
<hit index="9" value="2"/>
<hit index="10" value="2"/>
<hit index="11" value="2"/>
<hit index="15" value="2"/>
<hit index="16" value="2"/>
<hit index="17" value="2"/>
<hit index="18" value="2"/>
<hit index="19" value="2"/>
<hit index="20" value="2"/>
<hit index="21" value="21"/>
<hit index="22" value="21"/>
<hit index="23" value="21"/>
<hit index="25" value="2"/>
<hit index="26" value="3"/>
<hit index="28" value="1"/>
<hit index="31" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="11" total="14" coveredRatio="0.7857142857142857"/>
</method>
<chunk hits="21" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(null != coord);
            Contract.Requires(0 < DimensionSize && 0 < Dimensions);
            Contract.Requires(coord.Length == Dimensions);
            Contract.Ensures((uint)Math.Pow(DimensionSize, Dimensions) > Contract.Result<uint>());
            ]]></source>
</chunk>
<chunk hits="21" offset="0001" domain="userortestcode"><source><![CDATA[var index = 0;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            for (]]></source>
</chunk>
<chunk hits="21" offset="0003" domain="userortestcode"><source><![CDATA[int i = 1;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="23" offset="0060" domain="userortestcode"><source><![CDATA[i <= Dimensions]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[; ]]></source>
</chunk>
<chunk hits="2" offset="005c" domain="userortestcode"><source><![CDATA[i++]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[) ]]></source>
</chunk>
<chunk hits="3" offset="0007" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                // Loop invariant
                ]]></source>
</chunk>
<chunk hits="3" offset="0008" domain="userortestcode"><source><![CDATA[Contract.Assert(0 <= index && Grid.Length > index);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                // Make sure coordinates which exceed the bounds of any dimension are 'wrapped around'
                ]]></source>
</chunk>
<chunk hits="3" offset="0020" domain="userortestcode"><source><![CDATA[int c = coord[i - 1] % (int)DimensionSize;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                ]]></source>
</chunk>
<chunk hits="4" offset="002d" domain="userortestcode"><source><![CDATA[c = 0 <= c ? c : c + (int)DimensionSize;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                // Sum the the effect of each coordinate to find the correct position in the grid array
                ]]></source>
</chunk>
<chunk hits="2" offset="003d" domain="userortestcode"><source><![CDATA[index += c * (int)Math.Pow(DimensionSize, Dimensions - i);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="2" offset="005b" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="21" offset="0074" domain="userortestcode"><source><![CDATA[return (uint)index;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="21" offset="0078" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        ///   Translates an index number into a coordinate.
        /// </summary>
        /// <param name = "index"></param>
        /// <returns></returns>
        private int[] GetCoordinate(uint index) {
            Contract.Requires(index < Grid.Length);
            Contract.Ensures(Dimensions == Contract.Result<int[]>().Length);
            // As long as the grid is rectangular (not jagged) this is equivalent to converting
            // index from base 10 to base DimensionSize.
            return Tools.Math.DecimalToBase(index, DimensionSize, Dimensions).Reverse().ToArray();
        }

        /// <summary>
        ///   Returns the State Object of the cell.
        /// </summary>
        /// <param name = "coord"></param>
        /// <returns>The state of the cell with the given coordinates</returns>
        public State GetCellState(params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Ensures(Contract.Result<State>() != null);
            return GetCell(coord).State;
        }

        /// <summary>
        ///   Returns the rule of the cell with the given coordinates.
        ///   Note that one rule may consist of many rules.
        /// </summary>
        /// <param name = "coord">Coordinates</param>
        /// <returns>IRule</returns>
        [PexMethod]
        public Rule GetCellRule(params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Ensures(Contract.Result<Rule>() != null);
            return GetCell(coord).Rule;
        }

        /// <summary>
        ///   Sets the rule of the cell with the given coordinates.
        /// </summary>
        /// <param name = "rule"></param>
        /// <param name = "coord">Coordinates</param>
        public void SetCellRule(Rule rule, params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Requires(rule != null);
            GetCell(coord).Rule = rule;
        }

        /// <summary>
        ///   Add a trigger to the cell with the given coordinates.
        /// </summary>
        /// <param name = "trigger">Trigger</param>
        /// <param name = "coord">int[]</param>
        public void AddTrigger(Trigger trigger, params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Requires(trigger != null);
            Contract.Ensures(GetCell(coord).Triggers.Contains(trigger));
            GetCell(coord).Triggers.Add(trigger);
        }

        /// <summary>
        ///   Removes the trigger from the cell with the given coodinates.
        /// </summary>
        /// <param name = "trigger">Trigger</param>
        /// <param name = "coord">coordinates</param>
        public void RemoveTrigger(Trigger trigger, params int[] coord) {
            Contract.Requires(coord.Length == Dimensions);
            Contract.Requires(trigger != null);
            Contract.Ensures(!GetCell(coord).Triggers.Contains(trigger));
            GetCell(coord).Triggers.Remove(trigger);
        }

        /// <summary>
        ///   Evolve this Cellular Automaton - move one generation forward
        ///   by applying the rule(s) to the cells.
        /// </summary>
        public virtual void Evolve() {
            Contract.Ensures(Generation == Contract.OldValue(Generation) + 1);
            Contract.Ensures(Contract.ForAll(0, Grid.Length,
                                             i => Grid[i].State.CurrentState == Grid[i].State.NextState));
            // Calculate new states from current states
            for (uint index = 0; index < Grid.Length; index++)
                Grid[index].State.NextState = Grid[index].Rule.ApplyRule(GetCoordinate(index), this);

            // Commit new states
            foreach (Cell cell in Grid) cell.State.CurrentState = cell.State.NextState;
            Generation++;
        }

        /// <summary>
        ///   Indexer for the CA's grid of cells.
        /// </summary>
        /// <param name = "coord">coordinate</param>
        /// <returns>Cell at the given coordinate</returns>
        public virtual int this[params int[] coord] {
            get {
                Contract.Requires(coord.Length == Dimensions);
                return GetCell(coord).State.CurrentState;
            }
            set ]]></source>
</chunk>
<method name="set_Item" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" typeName="CellularAutomaton" token="6000146" module="AutomaTones.exe"><hits><hit index="0" value="21"/>
<hit index="1" value="21"/>
<hit index="2" value="21"/>
<hit index="3" value="21"/>
<hit index="4" value="21"/>
<hit index="5" value="21"/>
<hit index="6" value="21"/>
<hit index="7" value="21"/>
<hit index="8" value="21"/>
<hit index="9" value="42"/>
<hit index="10" value="21"/>
<hit index="11" value="21"/>
<hit index="12" value="21"/>
<hit index="13" value="42"/>
<hit index="15" value="1"/>
<hit index="16" value="1"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="4" total="4" coveredRatio="1"/>
</method>
<chunk hits="21" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                Contract.Requires(coord.Length == Dimensions);
                Contract.Ensures(GetCell(coord).State.CurrentState.Equals(value));
                ]]></source>
</chunk>
<chunk hits="21" offset="0001" domain="userortestcode"><source><![CDATA[Grid[GetIndex(coord)].State.CurrentState = value;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="63" offset="001c" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        }

        /// <summary>
        ///   For iterating over the entire grid.
        /// </summary>
        /// <returns></returns>
        [Pure]
        public IEnumerable<int> GridEnumerator() {
            return Grid.Select(cell => cell.State.CurrentState);
        }

        /// <summary>
        /// Returns a copy of the cellular automatons current grid of cells.
        /// </summary>
        /// <returns></returns>
        public Cell[] CopyOfGrid() {
            Contract.Ensures(Contract.ForAll(0, Grid.Length, i => Grid[i] == Contract.Result<Cell[]>()[i]));
            Cell[] copy = new Cell[Grid.Length];
            for (int i = 0; i < Grid.Length; i++) {
                Cell c = Grid[i];
                copy[i] = new Cell(c.Rule, c.State.SpaceMin, c.State.SpaceMax, c.State.CurrentState);
            }
            return copy;
        }

        [ContractInvariantMethod]
        // ReSharper disable UnusedMember.Local
        private void ObjInvar() {
            // ReSharper restore UnusedMember.Local
            Contract.Invariant(0 <= Generation);
            Contract.Invariant(0 < DimensionSize);
            Contract.Invariant(0 < Dimensions);
            Contract.Invariant(Grid.Length == (int)Math.Pow((double)DimensionSize, (double)Dimensions));
            Contract.Invariant(Contract.ForAll(Grid, c => null != c));
            Contract.Invariant(null != DefaultRule);
        }

        public override
            string ToString() {
            String s = "", t = "";
            foreach (int i in GridEnumerator()) {
                s += i;
                if (s.Length < 1 * DimensionSize) continue;
                t += s + "\n";
                s = "";
            }
            return t;
        }

        public object Clone() {
            return new CellularAutomaton(Dimensions, DimensionSize, DefaultRule) {
                Generation = Generation,
                Grid = CopyOfGrid()
            };
        }
    }

    /// <author>Anders Øland / anoe@itu.dk</author>
    /// <summary>
    /// A specialized Cellular Automaton for Wolfram one-dimensional automata.
    /// The grid does not have a torus shape (coordinates are not 'wrapper around'), instead the actual size of the grid is in fact
    /// larger than the specified size - so borderline cells can be processed.
    /// </summary>
    public class WolframAutomaton : CellularAutomaton {
        public uint ViewedSize { get; private set; }
        private readonly Cell[,] _viewedGrid;

        /// <summary>
        /// Create a new WolframAutomaton.
        /// </summary>
        /// <param name="wolframRule"></param>
        /// <param name="stateSpace">size of the state space, 2 = binary</param>
        /// <param name="viewedSize">size of the rectangular grid displaying the CA</param>
        /// <param name="actualSize">actual size of the CA in memmory</param>
        public WolframAutomaton(Rule wolframRule, int stateSpace, uint viewedSize = 64u, uint actualSize = 100u)
            : base(1, actualSize, wolframRule, 0, stateSpace - 1) {
            Contract.Requires(3 <= viewedSize);
            Contract.Requires(viewedSize <= actualSize);
            Contract.Requires(null != wolframRule);
            Contract.Requires(1 < stateSpace);
            ViewedSize = viewedSize;
            _viewedGrid = new Cell[ViewedSize, ViewedSize];
            base.GetCell((int)(actualSize / 2)).State.CurrentState = 1;
            UpdateViewedGrid();
            for (int i = 0; i < viewedSize; i++) Evolve();
        }

        /// <summary>
        ///   Returns a cell from the viewed two-dimensional grid.
        /// </summary>
        protected internal new Cell GetCell(params int[] coord) {
            Contract.Requires(coord.Length == 2);
            Contract.Ensures(Contract.Result<Cell>() != null);
            return _viewedGrid[coord[0], coord[1]];
        }

        public override sealed void Evolve() {
            base.Evolve();
            UpdateViewedGrid();
        }

        private void UpdateViewedGrid() {
            Contract.Requires(ViewedSize <= DimensionSize);
            Contract.Requires(null != _viewedGrid);
            Contract.Requires(null != Grid);
            for (int y = 0; y < ViewedSize; y++) {
                // Shift left
                for (int x = 0; x < ViewedSize - 1; x++) _viewedGrid[x, y] = _viewedGrid[x + 1, y];
                // Add new generation
                _viewedGrid[ViewedSize - 1, y] = base.GetCell((int)(ViewedSize / 2 - y + (int)DimensionSize / 2));
            }
        }
    }
}]]></source>
</chunk>
</document>
<document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Automaton Model\Cell.cs" renderPrefix="s2"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using Microsoft.Pex.Framework;

namespace AutomaTones.Model.Automaton_Model {
    /// <summary>
    /// A cell with a given state decided by one or more rules.
    /// Multiple triggers can be attached to react to different states - by publishing events.
    /// </summary>
    /// <author>Anders Øland / anoe@itu.dk</author>
    [Serializable]
    public class Cell {
        /// <summary>
        /// The state of the cell.
        /// </summary>
        public State State { get; protected internal set; }
        
        /// <summary>
        /// The rule governing the state & evolution of this cell
        /// </summary>
        public Rule Rule { get; protected internal set; }
        
        ]]></source>
</chunk>
<method name=".ctor" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.Cell" typeName="Cell" token="6000009" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="6" coveredRatio="0"/>
</method>
<chunk hits="0" offset="0000" domain="userortestcode"><source><![CDATA[private HashSet<Trigger> _triggers = new HashSet<Trigger>();]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        /// <summary>
        /// The set of triggers for this cell. May be empty.
        /// </summary>
        public HashSet<Trigger> Triggers {
            get { return _triggers; }
            protected internal set {
                Contract.Requires(null != value);
                Contract.Requires(Contract.ForAll<Trigger>(value, v => null != v));
                Contract.Ensures(Triggers == value);
                _triggers = value;
            }
        }

        /// <summary>
        /// Add a trigger to this cell.
        /// </summary>
        /// <param name="trigger"></param>
        public void AddTrigger(Trigger trigger) {
            Contract.Requires(null != trigger);
            Contract.Ensures(Triggers.Contains(trigger));
            _triggers.Add(trigger);
        }

        /// <summary>
        /// Remove a trigger from this cell.
        /// </summary>
        /// <param name="trigger"></param>
        public void RemoveTrigger(Trigger trigger) {
            Contract.Requires(null != trigger);
            Contract.Ensures(!Triggers.Contains(trigger));
            _triggers.Remove(trigger);
        }

        ]]></source>
</chunk>
<chunk hits="0" offset="000b" domain="userortestcode"><source><![CDATA[public Cell(Rule rule, int stateMin = 0, int stateMax = 1, int stateInit = 0)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0012" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            Contract.Requires(null != rule);
            Contract.Requires(stateMin < stateMax);
            Contract.Requires(stateMin <= stateInit && stateMax >= stateInit);
            Contract.Ensures(Rule.Equals(rule));
            Contract.Ensures(State.SpaceMin == stateMin && State.SpaceMax == stateMax && State.CurrentState == stateInit);
            ]]></source>
</chunk>
<chunk hits="0" offset="0013" domain="userortestcode"><source><![CDATA[Rule = rule;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="001b" domain="userortestcode"><source><![CDATA[State = new State(stateMin, stateMax, stateInit);]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="002b" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        [ContractInvariantMethod]
        private void ObjInvariant() {
            Contract.Invariant(null != State);
            Contract.Invariant(null != Rule);
        }
    }
}
]]></source>
</chunk>
</document>
<document src="C:\Users\Anders\Documents\automatones\Visual Studio\AutomaTones\AutomaTones\Model\Automaton Model\State.cs" renderPrefix="s3"><chunk hits="-1" domain="unknown"><source><![CDATA[using System;
using System.Diagnostics.Contracts;

namespace AutomaTones.Model.Automaton_Model {
    /// <author>Anders Øland / anoe@itu.dk</author>
    /// <summary>
    /// The state of a cell represented by a discrete value within a given state space.. 
    /// NOTE: Ideally this would be implemented using generics so states could also be decimals.
    /// NOTE: But due to C#'s lack of type constraint options on value types - this was omitted.
    /// </summary>
    [Serializable]
    public class State {
        /// <summary>
        /// The lower boundary of the state space.
        /// </summary>
        public int SpaceMin { get; protected set; }

        /// <summary>
        /// The upper boundary of the state space.
        /// </summary>
        public int SpaceMax { get; protected set; }

        private int _currentState;

        /// <summary>
        /// Current state.
        /// </summary>
        public int CurrentState {
            get { return _currentState; }
            protected internal set ]]></source>
</chunk>
<method name="set_CurrentState" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.State" typeName="State" token="60001cf" module="AutomaTones.exe"><hits><hit index="0" value="21"/>
<hit index="1" value="21"/>
<hit index="2" value="21"/>
</hits>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<chunk hits="21" offset="0000" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
                Contract.Ensures(CurrentState == value);
                ]]></source>
</chunk>
<chunk hits="21" offset="0001" domain="userortestcode"><source><![CDATA[_currentState = value;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="42" offset="0008" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        }

        private int _nextState;

        /// <summary>
        /// Next state.
        /// </summary>
        public int NextState {
            get { return _nextState; }
            protected internal set {
                Contract.Ensures(NextState == value);
                _nextState = value;
            }
        }

        /// <summary>
        /// Construct a new State. 
        /// </summary>
        /// <param name="spaceMin"></param>
        /// <param name="spaceMax"></param>
        /// <param name="currentState"></param>
        ]]></source>
</chunk>
<method name=".ctor" namespace="AutomaTones.Model.Automaton_Model" type="AutomaTones.Model.Automaton_Model.State" typeName="State" token="60001d2" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="4" coveredRatio="0"/>
</method>
<chunk hits="0" offset="0000" domain="userortestcode"><source><![CDATA[public State(int spaceMin = 0, int spaceMax = 1, int currentState = 0)]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[ ]]></source>
</chunk>
<chunk hits="0" offset="0007" domain="userortestcode"><source><![CDATA[{]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0008" domain="userortestcode"><source><![CDATA[SpaceMin = spaceMin;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0010" domain="userortestcode"><source><![CDATA[SpaceMax = spaceMax;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
            ]]></source>
</chunk>
<chunk hits="0" offset="0018" domain="userortestcode"><source><![CDATA[_currentState = currentState;]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[
        ]]></source>
</chunk>
<chunk hits="0" offset="001f" domain="userortestcode"><source><![CDATA[}]]></source>
</chunk>
<chunk hits="-1" domain="unknown"><source><![CDATA[

        [ContractInvariantMethod]
        private void ObjInvariant() {
            Contract.Invariant(SpaceMin < SpaceMax);
            Contract.Invariant(SpaceMin <= _currentState && SpaceMax >= _currentState);
            Contract.Invariant(SpaceMin <= _nextState && SpaceMax >= _nextState);
        }
    }
}]]></source>
</chunk>
</document>
<document src="(no sources available for some parts of AutomaTones, AutomaTones.Explorables)" missing="true"><method name="get_Dimensions" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000132" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="Create" type="AutomaTones.Model.Automaton_Model.CellularAutomatonFactory" token="6000006" module="&lt;In Memory Module&gt;"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="2" coveredRatio="0"/>
</method>
<method name="set_Generation" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000137" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_Dimensions" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000133" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_DimensionSize" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000135" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="get_DimensionSize" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000134" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
<method name="set_DefaultRule" type="AutomaTones.Model.Automaton_Model.CellularAutomaton" token="6000139" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_Rule" type="AutomaTones.Model.Automaton_Model.Cell" token="6000004" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_SpaceMin" type="AutomaTones.Model.Automaton_Model.State" token="60001cb" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_SpaceMax" type="AutomaTones.Model.Automaton_Model.State" token="60001cd" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="set_State" type="AutomaTones.Model.Automaton_Model.Cell" token="6000002" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="0" total="1" coveredRatio="0"/>
</method>
<method name="get_State" type="AutomaTones.Model.Automaton_Model.Cell" token="6000001" module="AutomaTones.exe"><hits/>
<coverage domain="userortestcode" unit="block" covered="1" total="1" coveredRatio="1"/>
</method>
</document>
</documents>
